package com.cefa.edu.service;

import com.cefa.edu.dao.EducationLevelMapper;
import com.cefa.edu.dao.UserMapper;
import com.cefa.edu.model.EducationLevel;
import com.cefa.edu.model.Role;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author huangzehui
 * @date 18-7-29 下午4:04
 */
@Service
public class IndexService {
    @Autowired
    EducationLevelMapper educationLevelMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 权限层级查询和一层级列表查询
     * @param id
     * @param level
     * @return
     */
    public List<EducationLevel> getEducationLevel(Integer id, Integer level) {
        return educationLevelMapper.getEducationLevel(id, level);
    }

    /**
     * 二、三层级列表查询
     * @param id
     * @return
     */
    public List<EducationLevel> getEducationLevelByPid(int id) {
        return educationLevelMapper.getEducationLevelByPid(id);
    }

    public List<EducationLevel> getEducationLevelByGid(int id) {
        return educationLevelMapper.getEducationLevelByGid(id);
    }
    public EducationLevel getEducationLevelById(int id) {
        return educationLevelMapper.getEducationLevelById(id);
    }

    public List<EducationLevel> getEducationLevelByIdNotIn(int userId, int level, String ids) {
        return educationLevelMapper.getEducationLevelByIdNotIn(userId, level, ids);
    }

    public List<EducationLevel> getEducationLevelByPidNotIn(int userId, int level, String ids) {
        return educationLevelMapper.getEducationLevelByPidNotIn(userId, level, ids);
    }

    public List<EducationLevel> getEducationLevelByIdAndPidNotIn(int userId, int level, String ids, String pIds) {
        return educationLevelMapper.getEducationLevelByIdAndPidNotIn(userId, level, ids, pIds);
    }

    @Transactional("transactionManager")
    public void  deleteDucationLevel(Integer levelId,Integer level) {
        if(level==1){
            educationLevelMapper.deleteUserEducationLevelByLevelId(levelId);//删除角色和该级层的关联数据
            educationLevelMapper.deleteDucationLevel(levelId);//删除级层数据
        }else if(level==3){//如果能删除
            //级联删除 1、先删除年级，在删除教育阶段
           List<EducationLevel> educationLevels= educationLevelMapper.getEducationLevelByPid(levelId);
           for (EducationLevel educationLevel:educationLevels){
               educationLevelMapper.deleteDucationLevel(educationLevel.getId());
               educationLevelMapper.deleteUserEducationLevelByLevelId(levelId);//删除角色和该级层的关联数据
           }
            educationLevelMapper.deleteDucationLevel(levelId);
            educationLevelMapper.deleteUserEducationLevelByLevelId(levelId);//删除角色和该级层的关联数据
        }

    }

    @Transactional("transactionManager")
    public int findUserEducationLevelByroleType(Integer schoolId,Integer roleType1,  Integer roleType2){
        return userMapper.findUserEducationLevelByroleType(schoolId,roleType1,roleType2);
    }

    @Transactional("transactionManager")
    public int findUserEducationLevelByroleTypeAndgId(Integer schoolId,Integer roleType1,  Integer roleType2 ,Integer  gId){
        return userMapper.findUserEducationLevelByroleTypeAndgId(schoolId,roleType1,roleType2,gId);
    }
    @Transactional("transactionManager")
    public int findUserEducationLevelByroleTypeAndpId(Integer schoolId,Integer roleType1,  Integer roleType2 ,Integer  pId){
        return userMapper.findUserEducationLevelByroleTypeAndpId(schoolId,roleType1,roleType2,pId);
    }
    @Transactional("transactionManager")
    public int findUserEducationLevelByroleTypeAndId(Integer schoolId,Integer roleType1,  Integer roleType2 ,Integer  id){
        return userMapper.findUserEducationLevelByroleTypeAndId(schoolId,roleType1,roleType2,id);
    }

    @Transactional("transactionManager")
    public int findUserEducationLevelByroleType2(Integer schoolId,Integer roleType) {
        return  userMapper.findUserEducationLevelByroleType2(schoolId,roleType);
    }
    @Transactional("transactionManager")
    public int findUserEducationLevelByroleTypeAndgId2(Integer schoolId,Integer roleType,Integer gId) {
        return  userMapper.findUserEducationLevelByroleTypeAndgId2(schoolId,roleType,gId);
    }
    @Transactional("transactionManager")
    public int findUserEducationLevelByroleTypeAndpId2(Integer schoolId,Integer roleType,Integer pId) {
        return  userMapper.findUserEducationLevelByroleTypeAndpId2(schoolId,roleType,pId);
    }
    @Transactional("transactionManager")
    public int findUserEducationLevelByroleTypeAndId2(Integer schoolId,Integer roleType,Integer id) {
        return  userMapper.findUserEducationLevelByroleTypeAndId2(schoolId,roleType,id);
    }



}